<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/23 0023
 * Time: 11:35
 */
namespace Api\Controller;
use Think\Controller;

class GoodsInfo1Controller extends Controller{
    //商品详情页
    public function goodsInfo(){
        $goods_id = I('goods_id');
        $goods_info = M('goods_list')->where('state=1 and goods_id='.$goods_id)->find();
        if($goods_info){
            $goods_info['goods_img'] =(array)json_decode($goods_info['goods_img']);
            $info =  M('goods_info')->where('goods_id='.$goods_info['goods_id'])->find();
            $goods_info['info'] =$info['goods_info'];
            $goods_info['seo_title'] =$info['seo_title'];
            $goods_info['seo_keywords'] =$info['seo_keywords'];
            $goods_info['seo_description'] =$info['seo_description'];
            $goods_info['price'] = M('goods_price')->where('goods_id='.$goods_info['goods_id'])->select();
            $score = M('goods_evaluate')->where('goods_id='.$goods_info['goods_id'])->avg('score');
            $goods_info['score'] = $score?$score:"5";
            $goods_info['evaluate_num'] = "".M('goods_evaluate')->where("goods_id=".$goods_id." and state=1 and evaluate_type=0")->count();
            $goods_info['img_evaluate_num'] = "".M('goods_evaluate')->where("goods_id=".$goods_id." and state=1 and evaluate_type=0 and img<>''")->count();
            $goods_info['chase_evaluate_num'] = "".M('goods_evaluate a')->join('agency_evaluate_reply b on a.evaluate_id=b.evaluate_id')->where("b.is_chase=1 and a.goods_id=".$goods_id." and a.state=1 and a.evaluate_type=0")->count();
            foreach ($goods_info['price'] as $k => $v){
                $region = M('region_list')->where('region_id='.$v['region_id'])->find();
                $goods_info['price'][$k]['region_name'] = $region['region_name'];
            }
            $this->ajaxReturn($goods_info,'JSON');
        }else{
            $data = array('info'=> '获取数据失败', 'status'=> '0',);
            $this->ajaxReturn($data,'JSON');
        }
    }

    public function evaluate(){
        $goods_id = I('goods_id');
        $page = I('page');
        $num = M('goods_evaluate')->where("goods_id=".$goods_id." and state=1 and evaluate_type=0")->count();
        $page_num = 1;
        $num1 = ceil($num/$page_num);
        $evaluate_list = M('goods_evaluate')->where("goods_id=".$goods_id." and state=1 and evaluate_type=0")->order('evaluate_time desc')->page($page,$page_num)->select();
        $vip_config = M('sys_configure')->where('configure_id in (2,3,4,5,6,7)')->select();
        $evaluate_arr = array();
        foreach ($evaluate_list as $k => $v){
            $user_info = M('user_list')->where('user_id='.$v['user_id'])->find();
            $growth = M('growth_user')->where("user_id=".$v['user_id'])->sum('growth');
            $chase = M('evaluate_reply')->where('is_chase=1 and reply_type=0 and evaluate_id='.$v['evaluate_id'].' and parent_reply_id=0')->order('reply_time desc')->find();

            $chase_reply_list = $chase?$this->evaluate_reply(0,$v['evaluate_id'],$chase['reply_id']):array();
            $chase_arr = $chase?array('chase'=> $chase['reply_content'], 'time'=> $chase['reply_time'], 'reply_list'=>$chase_reply_list,):array();

            $evaluate_arr['list'][$k]=array(
                'user_img'=>$user_info['user_img'],
                'user_name'=>$v['anonymous']==1?mb_substr($user_info['name'],0,1,'utf-8').'***'.mb_substr($user_info['name'],mb_strlen($user_info['name'],'utf-8')-1,1,'utf-8').'[匿名]':$user_info['name'],
                'score'=>$v['score'],
                'vip'=>$this->vip_config($vip_config,$growth),
                'evaluate'=>$v['evaluate'],
                'img'=>(array)json_decode($v['img']),
                'time'=>$v['evaluate_time'],
                'reply_list'=>$this->evaluate_reply(0,$v['evaluate_id'],0),
                'chase'=>$chase_arr
            );
        }
        $evaluate_arr['all_num'] = $num;
        $evaluate_arr['all_page'] = $num1;

        $this->ajaxReturn($evaluate_arr,'JSON');
    }

    public function img_evaluate(){
        $goods_id = I('goods_id');
        $page = I('page');
        $num = M('goods_evaluate')->where("goods_id=".$goods_id." and state=1 and evaluate_type=0 and img<>''")->count();
        $page_num = 1;
        $num1 = ceil($num/$page_num);
        $evaluate_list = M('goods_evaluate')->where("goods_id=".$goods_id." and state=1 and evaluate_type=0 and img<>''")->order('evaluate_time desc')->page($page,$page_num)->select();
        $vip_config = M('sys_configure')->where('configure_id in (2,3,4,5,6,7)')->select();
        $evaluate_arr = array();
        foreach ($evaluate_list as $k => $v){
            $user_info = M('user_list')->where('user_id='.$v['user_id'])->find();
            $growth = M('growth_user')->where("user_id=".$v['user_id'])->sum('growth');
            $chase = M('evaluate_reply')->where('is_chase=1 and reply_type=0 and evaluate_id='.$v['evaluate_id'].' and parent_reply_id=0')->order('reply_time desc')->find();
            $chase_reply_list = $chase?$this->evaluate_reply(0,$v['evaluate_id'],$chase['reply_id']):array();
            $chase_arr = $chase?array('chase'=> $chase['reply_content'], 'time'=> $chase['reply_time'], 'reply_list'=>$chase_reply_list,):array();

            $evaluate_arr['list'][$k]=array(
                'user_img'=>$user_info['user_img'],
                'user_name'=>$v['anonymous']==1?mb_substr($user_info['name'],0,1,'utf-8').'***'.mb_substr($user_info['name'],mb_strlen($user_info['name'],'utf-8')-1,1,'utf-8').'[匿名]':$user_info['name'],
                'score'=>$v['score'],
                'vip'=>$this->vip_config($vip_config,$growth),
                'evaluate'=>$v['evaluate'],
                'img'=>(array)json_decode($v['img']),
                'time'=>$v['evaluate_time'],
                'reply_list'=>$this->evaluate_reply(0,$v['evaluate_id'],0),
                'chase'=>$chase_arr
            );
        }
        $evaluate_arr['all_num'] = $num;
        $evaluate_arr['all_page'] = $num1;

        $this->ajaxReturn($evaluate_arr,'JSON');
    }

    public function chase_evaluate(){
        $goods_id = I('goods_id');
        $page = I('page');
        $num = M('goods_evaluate a')->join('agency_evaluate_reply b on a.evaluate_id=b.evaluate_id')->where("b.is_chase=1 and a.goods_id=".$goods_id." and a.state=1 and a.evaluate_type=0")->count();
        $page_num = 1;
        $num1 = ceil($num/$page_num);
        $evaluate_list = M('goods_evaluate a')->join('agency_evaluate_reply b on a.evaluate_id=b.evaluate_id')->where("b.is_chase=1 and a.goods_id=".$goods_id." and a.state=1 and a.evaluate_type=0")->order('a.evaluate_time desc')->page($page,$page_num)->select();
        $vip_config = M('sys_configure')->where('configure_id in (2,3,4,5,6,7)')->select();
        $evaluate_arr = array();
        foreach ($evaluate_list as $k => $v){
            $user_info = M('user_list')->where('user_id='.$v['user_id'])->find();
            $growth = M('growth_user')->where("user_id=".$v['user_id'])->sum('growth');
            $chase = M('evaluate_reply')->where('is_chase=1 and reply_type=0 and evaluate_id='.$v['evaluate_id'].' and parent_reply_id=0')->order('reply_time desc')->find();
            $chase_reply_list = $chase?$this->evaluate_reply(0,$v['evaluate_id'],$chase['reply_id']):array();
            $chase_arr = $chase?array('chase'=> $chase['reply_content'], 'time'=> $chase['reply_time'], 'reply_list'=>$chase_reply_list,):array();

            $evaluate_arr['list'][$k]=array(
                'user_img'=>$user_info['user_img'],
                'user_name'=>$v['anonymous']==1?mb_substr($user_info['name'],0,1,'utf-8').'***'.mb_substr($user_info['name'],mb_strlen($user_info['name'],'utf-8')-1,1,'utf-8').'[匿名]':$user_info['name'],
                'score'=>$v['score'],
                'vip'=>$this->vip_config($vip_config,$growth),
                'evaluate'=>$v['evaluate'],
                'img'=>(array)json_decode($v['img']),
                'time'=>$v['evaluate_time'],
                'reply_list'=>$this->evaluate_reply(0,$v['evaluate_id'],0),
                'chase'=>$chase_arr
            );
        }
        $evaluate_arr['all_num'] = $num;
        $evaluate_arr['all_page'] = $num1;

        $this->ajaxReturn($evaluate_arr,'JSON');
    }



    private function evaluate_reply($reply_type,$evaluate_id,$parent_id){
        $reply_list = M('evaluate_reply')->where('is_chase=0 and reply_type='.$reply_type.' and evaluate_id='.$evaluate_id.' and parent_reply_id='.$parent_id)->order('reply_time desc')->select();
        $reply_arr= array();
        foreach ($reply_list as $k=>$v){
            $user_name = "";
            switch ($v['user_type']){
                case 0:
                    $user_info = M('user_list')->where('user_id='.$v['user_id'])->find();
                    $user_name = $user_info['name'];
                    break;
                case 1:
                    $user_info = M('user_supplier')->where('operator_id='.$v['user_id'])->find();
                    $user_name = "<span style='color:#ff0000 '>[客服]</span>".$user_info['name'];
                    break;
                case 2:
                    $user_info = M('user_admin')->where('admin_id='.$v['user_id'])->find();
                    $user_name = "<span style='color:#ff0000 '>[客服]</span>".$user_info['name'];
                    break;
            }
            $reply_arr[$k] = array(
                'name'=>$user_name,
                'reply_content'=>$v['reply_content'],
                'reply_time'=>$v['reply_time'],
                'reply_list'=>$this->evaluate_reply(1,$evaluate_id,$v['reply_id'])
            );
            //$reply_arr[$k] =
        }
        return $reply_arr;
    }

    private function vip_config($vip_config,$growth){
        $vip = 0;
        foreach ($vip_config as $k=>$v){
            switch ($v['configure_id']){
                case 2:
                    $vip = $growth>$v['configure_value'] && $vip<1?1:$vip;
                    break;
                case 3:
                    $vip = $growth>$v['configure_value'] && $vip<2?2:$vip;
                    break;
                case 4:
                    $vip = $growth>$v['configure_value'] && $vip<3?3:$vip;
                    break;
                case 5:
                    $vip = $growth>$v['configure_value'] && $vip<4?4:$vip;
                    break;
                case 6:
                    $vip = $growth>$v['configure_value'] && $vip<5?5:$vip;
                    break;
                case 7:
                    $vip = $growth>$v['configure_value'] && $vip<6?6:$vip;
                    break;
            }
        }

        return $vip."";
    }

    public function advertisement(){
        $goods_list = M('goods_list')->where('state=1')->order('rand()')->limit(7)-> select();
        $goods_arr = array();
        foreach ($goods_list as $k => $v){
            $goods_price = M('goods_price')->where('goods_id='.$v['goods_id'])->find();
            $img = (array)json_decode($v['goods_img']);
            $goods_arr[]=array(
                'goods_id'=>$v['goods_id'],
                'goods_name'=>$v['goods_name'],
                'goods_img'=>$img[0],
                'goods_price'=>$goods_price['price']
            );
        }


        $goods_list1 = M('goods_list')->where('state=1')->order('rand()')->limit(6)-> select();
        $goods_arr1 = array();
        foreach ($goods_list1 as $k => $v){
            $goods_price = M('goods_price')->where('goods_id='.$v['goods_id'])->find();
            $img = (array)json_decode($v['goods_img']);
            $goods_arr1[]=array(
                'goods_id'=>$v['goods_id'],
                'goods_name'=>$v['goods_name'],
                'goods_img'=>$img[0],
                'goods_price'=>$goods_price['price']
            );
        }

        $advertisement = array(
            'advertisement1'=>$goods_arr,
            'advertisement2'=>$goods_arr1
        );
        $this->ajaxReturn($advertisement,'JSON');
    }
}